﻿using DingTalkByHttp.Helper;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DingTalkByHttp
{
    public class WebApiHelper
    {
        public static string CallGetWebApi(string url, Dictionary<string, string> param)
        {
            HttpRequestUntil httpHelperGet = new HttpRequestUntil("GET", "application/x-www-form-urlencoded");
            int index = 0;
            foreach (var item in param)
            {
                if (index == 0)
                {
                    url += ("?" + UrlEncodeUTF8(item.Key) + "=" + UrlEncodeUTF8(item.Value));
                }
                else
                {
                    url += ("&" + UrlEncodeUTF8(item.Key) + "=" + UrlEncodeUTF8(item.Value));
                }

                index += 1;
            }
            var json = httpHelperGet.GetHttpContext(url);
            return json;
        }



        public static string CallPostWebApi(string url, string postData)
        {
            HttpRequestUntil httpHelperPost = new HttpRequestUntil("POST", "application/json;charset=utf-8");
            var json = httpHelperPost.Response(url, postData, 20000);
            return json;
        }
        static String proxyHost = "127.0.0.1";
        static int proxyPort = 8087;

        /// <summary>
        /// 编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static String UrlEncodeUTF8(String str)
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
            for (int i = 0; i < byStr.Length; i++)
            {
                sb.Append(@"%" + Convert.ToString(byStr[i], 16));
            }
            return (sb.ToString());
        }

        public static string Post(string url, Dictionary<string, string> headers, string postData)
        {
            var client = new RestClient();
            var request = new RestRequest(url, Method.Post);
            request.AddHeader("Content-Type", "application/json");
            if (headers.Any())
            {
                request.AddHeaders(headers);
            }
            request.AddJsonBody(postData);
            var response = client.Execute(request);
            return response.Content;
        }
        public static string Get(string url, Dictionary<string, string> headers, Dictionary<string, string> paras)
        {
            var client = new RestClient();
            var request = new RestRequest(url, Method.Get);
            foreach (var para in paras)
            {
                request.AddQueryParameter(para.Key, para.Value);
            }
            if (headers.Any())
            {
                request.AddHeaders(headers);
            }
            var response = client.Execute(request);
            return response.Content;
        }
    }
}
